package com.lei.day02lx.network.schedulers;

import androidx.annotation.Nullable;

import io.reactivex.ObservableTransformer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Lc
 * @description: 线程切换
 * @date :2020/11/10 15:15
 */
public class SchedulerProvider implements BaseSchedulerProvider {

    @Nullable
    private static SchedulerProvider INSTANCE;

    private SchedulerProvider() {

    }

    public static synchronized SchedulerProvider getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SchedulerProvider();
        }
        return INSTANCE;
    }

    @Nullable
    @Override
    public Scheduler computation() {
        return Schedulers.computation();
    }

    @Nullable
    @Override
    public Scheduler io() {
        return Schedulers.io();
    }

    @Nullable
    @Override
    public Scheduler ui() {
        return AndroidSchedulers.mainThread();
    }

    /**
     * 申请统一使用Schedulers
     * @param <T>
     * @return
     */
    @Nullable
    @Override
    public <T> ObservableTransformer<T, T> applySchedulers() {
        return observalbe -> observalbe.subscribeOn(io()).observeOn(ui());
    }
}
